{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/36_quality_mosaic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# How to find out the greenest day of the year"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Define a region of interest (ROI)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ee.FeatureCollection(\"users/giswqs/public/countries\")\n",
    "Map.addLayer(countries, {}, \"countries\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi = countries.filter(ee.Filter.eq(\"id\", \"USA\"))\n",
    "Map.addLayer(roi, {}, \"roi\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Filter ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2019-01-01\"\n",
    "end_date = \"2019-12-31\"\n",
    "\n",
    "l8 = (\n",
    "    ee.ImageCollection(\"LANDSAT/LC08/C01/T1_TOA\")\n",
    "    .filterBounds(roi)\n",
    "    .filterDate(start_date, end_date)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(l8.size().getInfo())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "## Create a median composite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "median = l8.median()\n",
    "\n",
    "visParams = {\n",
    "    \"bands\": [\"B4\", \"B3\", \"B2\"],\n",
    "    \"min\": 0,\n",
    "    \"max\": 0.4,\n",
    "}\n",
    "\n",
    "Map.addLayer(median, visParams, \"Median\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "## Define functions to add time bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "def addNDVI(image):\n",
    "    ndvi = image.normalizedDifference([\"B5\", \"B4\"]).rename(\"NDVI\")\n",
    "    return image.addBands(ndvi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "def addDate(image):\n",
    "    img_date = ee.Date(image.date())\n",
    "    img_date = ee.Number.parse(img_date.format(\"YYYYMMdd\"))\n",
    "    return image.addBands(ee.Image(img_date).rename(\"date\").toInt())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "def addMonth(image):\n",
    "    img_date = ee.Date(image.date())\n",
    "    img_doy = ee.Number.parse(img_date.format(\"M\"))\n",
    "    return image.addBands(ee.Image(img_doy).rename(\"month\").toInt())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "def addDOY(image):\n",
    "    img_date = ee.Date(image.date())\n",
    "    img_doy = ee.Number.parse(img_date.format(\"D\"))\n",
    "    return image.addBands(ee.Image(img_doy).rename(\"doy\").toInt())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "## Map over an ImageCollection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "withNDVI = l8.map(addNDVI).map(addDate).map(addMonth).map(addDOY)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "## Create a quality mosaic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "greenest = withNDVI.qualityMosaic(\"NDVI\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "greenest.bandNames().getInfo()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25",
   "metadata": {},
   "source": [
    "## Display the max value band"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "ndvi = greenest.select(\"NDVI\")\n",
    "palette = [\n",
    "    \"#d73027\",\n",
    "    \"#f46d43\",\n",
    "    \"#fdae61\",\n",
    "    \"#fee08b\",\n",
    "    \"#d9ef8b\",\n",
    "    \"#a6d96a\",\n",
    "    \"#66bd63\",\n",
    "    \"#1a9850\",\n",
    "]\n",
    "Map.addLayer(ndvi, {\"palette\": palette}, \"NDVI\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(greenest, visParams, \"Greenest pixel\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "## Display time bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(\n",
    "    greenest.select(\"month\"),\n",
    "    {\"palette\": [\"red\", \"blue\"], \"min\": 1, \"max\": 12},\n",
    "    \"Greenest month\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(\n",
    "    greenest.select(\"doy\"),\n",
    "    {\"palette\": [\"brown\", \"green\"], \"min\": 1, \"max\": 365},\n",
    "    \"Greenest doy\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
